<?php
/**
 * Message Grid Component
 * 
 * Master Grid list viewer use across the application
 */

use Orx\Mailer\MailBox\Message;
use Fox\Behaviors;
use Fox\Generator;

// Initialize Options
$mode = !empty($params['mode']) ? $params['mode'] : Xview::instance()->val('mode');
$area = !empty($params['area']) ? $params['area'] : Xview::instance()->val('area');
$summary = !empty($params['summary']) ? $params['summary'] : Xview::instance()->val('summary');
$status = !empty($params['status']) ? $params['status'] : Xview::instance()->val('status');
$container = !empty($params['container']) ? $params['container'] : Xview::instance()->val('container','contentCol');
$mode = !empty($params['mode']) ? $params['mode'] : Xview::instance()->val('mode',false);
$reference = !empty($params['container']) ? $params['container'] : Xview::instance()->val('reference');

// Build the grid
$grid = new \Fox\Grid('messages');
$grid->layout()->noHeader();
$grid->keyFields(array('messageid' => true));
$grid->fieldcat(array('email_from','labels','subject','attachements','received'));
$grid->fieldcat()->setGenerators(array(
	'email_from' => Generator::useFunction(function($data){
		// Check for Reference ?
		if(empty($data['reference'])) {
			if($data['reference_class_id'] == \Orx\Mailer\MailBox\Message::MAILBOX_REFERENCE) {
				try {
					$mailbox = Fox\Dao::load(\Orx\User\MailBox::className(),$data['idusr'].':'.$data['boxid']);
					$output = '<span class="label label-important" title="'.$data['email'].'">'.$mailbox->name.'</span>';
				} catch(\Fox\Object\Exception\NotExist $e) {
					$output = '<span class="label label-important" title="'.$data['email'].'">'.$data['email'].'</span>';
				}
				
			} else {
				$findedInDB = \Hive\Ressource::findEmailDuplicates($data['email']);
				if(count($findedInDB) == 1 ) {
					$output = '<a class="label label-info haslink" href="?mod=ressource&act=main&rid='.$findedInDB[0]['ressource_id'].'&area=messages" title="'.$data['email'].'">'.$findedInDB[0]['first_name'].' '.$findedInDB[0]['last_name'].'</a>';
				} elseif(count($findedInDB) > 1) {
					$output = '<span class="label label-warning" title="'.$data['email'].'">'.$findedInDB[0]['first_name'].' '.$findedInDB[0]['last_name'].'</span>';
				} else {
					$name = !empty($data['name']) ?  $data['name'] : $data['email'];
					$output = '<span class="label" title="'.$data['email'].'">'.$name.'</span>';
				}
			}
		}  else {
			$output = '<a class="label label-success haslink" href="?mod=ressource&act=main&rid='.$data['reference'].'&area=messages" title="'.$data['email'].'">'.$data['name'].'</a>';
		}
		if($data['status'] == \Orx\Mailer\MailBox\Message::STATUS_DRAFT) {
			$output .= '<span class="text-error" style="margin-left: 5px;">'.Msg(\Orx\Mailer\Messages::BUNDLE,\Orx\Mailer\Messages::DRAFT);
		}
		return $output;
	}),
	'attachements' => Generator::useFunction(function($data){
		if(!empty($data['attachements'])) {
			return '<span style="margin-right:5px"><i class="icon icon-file" title="'.$data['attachements'].'"></i></span>';
		}
	}),
	'received' => Generator::useFunction(function($data){
		$today = new \DateTime();
		$receivedData = new \DateTime($data['received_date']);
		if($receivedData->format('Ymd') == $today->format('Ymd')) {
			// Today we display only the time
			return $receivedData->format('H:i');
		} else {
			if($receivedData->format('Y') == $today->format('Y')) {
				// Older display Month and day
				return \Fox\Utilities\Dates::dateTimeToDayMonthString($data['received_date']);
			} else {
				// Older display Month and day + Year
				return \Fox\Utilities\Dates::dateTimeToMonthYearString($data['received_date']);
			}
			
		}
	}),
	'labels' => Generator::useFunction(function($data){
		if(!empty($data['labels'])) {
			foreach($data['labels'] as $labelData ){
				$label = $labelData['label'];
				$link = '?mod=missions&act=main&area=messages&missionid='.$labelData['id'];
				$inlineid = $labelData['inlineid'];
			//	$delete = '<span class="remove {\'target\' : \'?mod=messages&act=references&sac=unassign&inlineid=$inlineid\'}">&times;</span>';
				return  '<a class="label label-inverse" href="'.$link.'" style="font-size: 10px;margin-right: 2px;">'.$label.'</a>';
			}
		}
	
	}),
	'subject' => Generator::useFunction(function($data) use($mode,$area,$status,$container,$reference,$summary){
		if($summary) {
			$content = Message::summary($data['content_html']);
			$output = '<p class="message-w-summary"><span class="subject">'.$data['subject'].'</span><br>';
			$output .= '<span class="message-summary">'.$content.'</span></p>';
			return $output;
		} else {
			return $data['subject'];
		}
		
	})
));
$grid->fieldcat()->field('email_from')->setClass('message-from-list');
$grid->fieldcat()->field('subject')->setClass('message-subject-list');
$grid->fieldcat()->field('labels')->setClass('message-labels-list');
foreach(array('subject','email_from','labels','received') as $field) {
	$grid->fieldcat()->field($field)->dynamicBehaviors(Behaviors::action(
		Generator::url('messages','viewer',null,array(
			'messageid' => Generator::useElement('messageid'),
			'mode' => $mode,
			'area' => $area,
			'status' => $status,
			'summary' => $summary,
			'container' => $container,
			'reference' => $reference)),
		false,
		null,
		null,
		false,
		'POST',
		null,
		null,
		$container,
		false
	));
}

$grid->fieldcat()->setZebra('message-email-row clickable','message-email-row clickable');
// Generate Bold Behavior
$grid->fieldcat()->setClassField(Generator::useFunction(function($data){
	if($data['status'] == \Orx\Mailer\MailBox\Message::STATUS_UNREAD) {
		return 'bold';
	}
}));
return $grid;